package com.jwthhealth_pub.old.subscribe;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.RegularUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.subscribe.adapter.CloseShopAdapter;
import com.jwthhealth_pub.old.subscribe.address.SubscribeHistoryAddressActivity;
import com.jwthhealth_pub.old.subscribe.model.SubscribeCommitResponse;
import com.jwthhealth_pub.old.subscribe.model.SubscribeIndex;
import com.jwthhealth_pub.old.subscribe.model.SubscribeList;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 预约页面
 * Created by Administrator on 2017/7/12.
 */

public class SubscribeActivity extends BaseActivity implements SubscribeContect.view {

    private static String TAG = makeLogTag(SubscribeActivity.class);

    public static final int REQUEST_PERMISSION = 11;

    @BindView(R.id.root_layout)
    RelativeLayout rootLayout;
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.btn_subscribe_confirm)
    Button btnSubscribeConfirm;
    @BindView(R.id.rv_close_shops)
    RecyclerView rvCloseShops;
    @BindView(R.id.tv_summary_text)
    TextView tvSummaryText;
    @BindView(R.id.tv_project_summary)
    TextView tvProjectSummary;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_id)
    EditText edId;
    @BindView(R.id.tv_choice_shop_text)
    TextView choiceShopText;
    @BindView(R.id.tv_choice_date_text)
    TextView choiceDateText;
    @BindView(R.id.tv_choice_project_text)
    TextView choiceProgectText;
    @BindView(R.id.tv_choice_time_text)
    TextView choiceTimeText;
    @BindView(R.id.tv_location_current)
    TextView currAddText;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.btn_nav)
    RelativeLayout navBtn;

    private SubscribeContect.presenter mPresneter;

    private String mChoiceDate, mChoiceTime;
    private boolean isLoadShops;
    private List<SubscribeIndex.DataBean> mData;
    private SubscribeIndex.DataBean mShop;
    private SubscribeIndex.DataBean.SonBean mProject;
    private SubscribeList.DataBean mCacheData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe);

        ButterKnife.bind(this);

        mPresneter = new SubscribePresenter(this);

        mPresneter.location();
//        String[] permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
//        if (Build.VERSION.SDK_INT >= 23 && (!(checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED))) {
//            requestPermissions(permissions, REQUEST_PERMISSION);
//        } else {
//            mPresneter.getLocation();
//        }

        //todo bug:页面没有停留在初始位置
        scrollView.smoothScrollTo(0, 0);
    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {
                Intent intent = new Intent(SubscribeActivity.this, SubscribeListActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    public void refreshUi(List<SubscribeIndex.DataBean> _data) {

        mData = _data;
        Log.d(TAG, "1:" + 1);
        isLoadShops = true;
        rvCloseShops.setVisibility(View.VISIBLE);
        rvCloseShops.setLayoutManager(new LinearLayoutManager(this));
        List<SubscribeIndex.DataBean> closeShops = new ArrayList<>();
        if (_data.size() > 0) {
            closeShops.add(mData.get(0));
        }
        if (_data.size() > 1) {
            closeShops.add(mData.get(1));
        }
        rvCloseShops.setAdapter(new CloseShopAdapter(this, closeShops));

        titleLayout.setRightText(getResources().getString(R.string.subscribe_history_list));
        titleLayout.setTitle("在线预约");

        mPresneter.getCacheData();

        missLoadProgressbar();
    }

    @Override
    public void refreshCurAdd(String add) {
        currAddText.setText(add);
    }

    @Override
    public void choiceShop(SubscribeIndex.DataBean _shop) {
        if (choiceShopText.getText().equals(_shop.getName()))
            return;

        navBtn.setVisibility(View.VISIBLE);
        mShop = _shop;
        choiceShopText.setText(mShop.getName());
        choiceProgectText.setText("");
        mProject = null;
        choiceDateText.setText("");
        choiceTimeText.setText("");
        mChoiceTime = null;
        clearData();
        missLoadProgressbar();
    }

    private void clearData() {
        tvSummaryText.setVisibility(View.GONE);
        tvProjectSummary.setVisibility(View.GONE);
        choiceProgectText.setText("");
        choiceTimeText.setText("");
        choiceDateText.setText("");
        mChoiceTime = null;
        mChoiceDate = null;
        mProject = null;
    }

    @Override
    public void showDateList() {
        final BottomSheetDialog dateBottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_date_list, null);
        dateBottomSheetDialog.setContentView(view);

        //处理显示不全的问题
        View parent = (View) view.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);

        final DatePicker datePicker = view.findViewById(R.id.datepicker);
        datePicker.setMinDate(System.currentTimeMillis() + 86400000L);
        datePicker.setMaxDate(System.currentTimeMillis() + 86400000L * 30);

        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mChoiceDate = "" + datePicker.getYear() + (datePicker.getMonth() + 1) + (datePicker.getDayOfMonth() + 1);
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
                mChoiceDate = "" + datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + (datePicker.getDayOfMonth());
                choiceDateText.setText(mChoiceDate);
//                try {
//                    Date choiceDate = simpleDateFormat.parse(mChoiceDate);
//                    long difference = choiceDate.getTime() - System.currentTimeMillis();
//                    mChoiceDate = "" + datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + (datePicker.getDayOfMonth());
//                    choiceDateText.setText(mChoiceDate);
//                    if (difference > 0 || Math.abs(difference) < 86400000) {
//                        mChoiceDate = "" + datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + (datePicker.getDayOfMonth());
//                        choiceDateText.setText(mChoiceDate);
//                    } else {
//                        LogUtil.toast(getResources().getString(R.string.subscribe_alert_date));
//                    }

//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }


                dateBottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dateBottomSheetDialog.dismiss();
            }
        });

        dateBottomSheetDialog.show();
    }

    @Override
    public void showProjectList() {
        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_shop_list, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);
        LoopView loopView = view.findViewById(R.id.loopView);

        final ArrayList<String> list = new ArrayList<>();
        final List<SubscribeIndex.DataBean.SonBean> son = mShop.getSon();
        if (son == null) return;
        for (int i = 0; i < son.size(); i++) {
            list.add(son.get(i).getPname());
        }

        mProject = son.get(0);
//        loopView.setLayoutManager(new LinearLayoutManager(this));
//        loopView.setAdapter(new ProjectInnerAdapter(this, list, son));

        // TODO: 2017/11/8  
        //设置是否循环播放
        loopView.setNotLoop();
        //设置原始数据
        loopView.setItems(list);

        //设置初始位置
        loopView.setInitPosition(0);

        //滚动监听
        mProject = son.get(0);
        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
                mProject = son.get(index);
            }
        });

        shopbottomSheetDialog.show();
        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvSummaryText.setVisibility(View.VISIBLE);
//                tvProjectSummary.setVisibility(ViewHolder.VISIBLE);
                choiceProgectText.setVisibility(View.VISIBLE);
                choiceProgectText.setText(mProject.getPname());
                tvProjectSummary.setText(mProject.getPtitle());
                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });
    }

    @Override
    public void showShopList() {
        Log.d(TAG, "1:" + 2);
        if (mData == null || mData.size() <= 0) {
            LogUtil.toast("未获取到门店数据");
            return;
        }

        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_shop_list, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);
        LoopView loopView = view.findViewById(R.id.loopView);

        //门店数据
        final ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            list.add(mData.get(i).getName());
        }

        // TODO: 2017/11/8  
        //设置是否循环播放
        loopView.setNotLoop();
        //设置原始数据
        loopView.setItems(list);

        //设置初始位置
        loopView.setInitPosition(0);

        //滚动监听
//        mTempChoiceShop = list.get(0);
        mShop = mData.get(0);
        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
                mShop = mData.get(index);
            }
        });

        shopbottomSheetDialog.show();
        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已经选择的门店
                choiceShop(mShop);
                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });
    }

    @Override
    public void showTimeList() {
        if (mProject == null)
            return;

        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_shop_list, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);
        LoopView loopView = view.findViewById(R.id.loopView);

        final List<String> time_list1 = mProject.getTime_list1();
        List<String> _timeList = new ArrayList<>();
        for (int i = 0; i < time_list1.size(); i++) {
            String _s = time_list1.get(i);
            if (_s.contains("|")) {
                _s = _s.replace("|", "");
                _s = _s + "(已有预约)";
            }
            _timeList.add(_s);
        }

        // TODO: 2017/11/8
        loopView.setNotLoop();
        loopView.setItems(_timeList);
        loopView.setInitPosition(0);
        mChoiceTime = time_list1.get(0);
        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
            }
        });

        shopbottomSheetDialog.show();
        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已经选择的门店

                choiceTimeText.setText(mChoiceTime);
                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });
    }

    @Override
    public void refreshCache(SubscribeList.DataBean data) {
        mCacheData = data;

        if (data.getAstatus().equals("4"))
            titleLayout.setTitle("重新预约");
        else
            titleLayout.setTitle("修改预约");
        titleLayout.setRightText("");

        String _shopId = data.getJwth_store_id();
        for (int i = 0; i < mData.size(); i++) {
            SubscribeIndex.DataBean _shop = mData.get(i);
            if (_shop.getId().equals(_shopId)) {
                mShop = _shop;
                for (int j = 0; j < mShop.getSon().size(); j++) {
                    SubscribeIndex.DataBean.SonBean _project = _shop.getSon().get(j);
                    if (data.getJwth_store_project_id().equals(_project.getId())) {
                        mProject = _project;
                        choiceShopText.setText(data.getName());
                        choiceProgectText.setText(data.getPname());
                        edName.setText(data.getAname());
                        edPhone.setText(data.getAphone());
                        edId.setText(data.getNo_card());
                        return;
                    }
                }
            }
        }
//        choiceShopText.setText(data.getName());
//        choiceProgectText.setText(data.getPname());
    }

    @Override
    public void showBottomMenu(int type) {

    }

//    @Override
//    public void showBottomMenu(int type) {
//        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
//
//    }

    public void showTimeList(List<String> data) {
        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_shop_list, null);
        shopbottomSheetDialog.setContentView(view);
        TextView completeBtn = view.findViewById(R.id.tv_complete);
        ImageView closeBtn = view.findViewById(R.id.btn_close);
        final LoopView loopView = view.findViewById(R.id.loopView);

        final List<String> _data = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            String _s = data.get(i);
            if (_s.contains("|")) {
                _s = _s.replace("|", "");
                _s = _s + "(已有预约)";
            }
            _data.add(_s);
        }

        loopView.setNotLoop();
        loopView.setItems(_data);
        loopView.setInitPosition(0);
        mChoiceTime = _data.get(0);
        loopView.setListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
            }
        });

        shopbottomSheetDialog.show();
        completeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已经选择的门店
                mChoiceTime = _data.get(loopView.getSelectedItem());
                if (mChoiceTime.contains("(")) {
                    LogUtil.toast("已有预约，请选择其他时间段");
                    return;
                }
                choiceTimeText.setText(mChoiceTime);
                shopbottomSheetDialog.dismiss();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });
    }

    @Override
    public void setmPresenter(Object mPresenter) {

    }

    @Override
    public Object getPresenter() {
        if (mPresneter == null) {
            mPresneter = new SubscribePresenter(this);
        }

        return mPresneter;
    }

    @Override
    public void toast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LogUtil.toast(msg);
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mPresneter.location();
//            mPresneter.getLocation();
        } else {
            //手动点击了拒绝
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.help);
            builder.setMessage(R.string.help_text);
            builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//            intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
            });
            builder.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    LogUtil.toast(getString(R.string.subscribe_permission_hint));
                }
            });

            builder.show();

        }
    }

    @OnClick({R.id.tv_summary_text, R.id.btn_nav, R.id.layout_location, R.id.tv_name, R.id.tv_phone, R.id.tv_id, R.id.cv_one, R.id.cv_two, R.id.cv_thi, R.id.cv_for, R.id.cv_fiv, R.id.cv_six, R.id.btn_subscribe_confirm, R.id.arrow_down, R.id.tv_location_current})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_summary_text:
                //项目简介
                Intent _intent = new Intent(this, SubscribeSummaryActivity.class);
                _intent.putExtra("title", mProject.getPname());
                _intent.putExtra("id", mProject.getId());
                startActivity(_intent);
                break;
            case R.id.tv_name:
                edName.setFocusable(true);
                edName.setFocusableInTouchMode(true);
                edName.requestFocus();
                showKeyBoard();
                break;
            case R.id.tv_phone:
                edPhone.setFocusable(true);
                edPhone.setFocusableInTouchMode(true);
                edPhone.requestFocus();
                showKeyBoard();
                break;
            case R.id.tv_id:
                edId.setFocusable(true);
                edId.setFocusableInTouchMode(true);
                edId.requestFocus();
                showKeyBoard();
                break;
            case R.id.cv_one:
                break;
            case R.id.cv_two:
                if (isLoadShops) {
                    showShopList();
                }
                break;
            case R.id.cv_thi:
                if (isLoadShops) {
                    showDateList();
                }
                break;
            case R.id.cv_for:
                if (isLoadShops && mShop != null)
                    showProjectList();
                else
                    Toast.makeText(this, "请选择预约门店", Toast.LENGTH_SHORT).show();
                break;
            case R.id.cv_fiv:
//                showTimeList();
                if (isLoadShops) {

                    if (mProject == null) {
                        Toast.makeText(this, "请选择预约项目", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (mChoiceDate == null) {
                        Toast.makeText(this, "请选择预约日期", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    mPresneter.getTimeList(mProject.getId(), mShop.getId(), mChoiceDate);
                }
                break;
            case R.id.cv_six:
                break;
            case R.id.btn_subscribe_confirm:

                Resources resources = getResources();

                if (mShop == null) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_shop));
                    return;
                }

                if (mChoiceDate == null || mChoiceDate.length() <= 0) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_date));
                    return;
                }

                if (mProject == null) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_project));
                    return;
                }

                if (mChoiceTime == null || mChoiceTime.length() <= 0) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_time));
                    return;
                }

                String name = edName.getText().toString().trim();
                if (name.length() == 0) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_name));
                    return;
                }

                String phone = edPhone.getText().toString().trim();
                if (phone.length() <= 0) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_phone));
                    return;
                } else if (!RegularUtil.checkPhoneNumber(phone)) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_phone_correct));
                    return;
                }

                String id = edId.getText().toString().trim();
                if (id.length() == 0) {
                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_id));
                    return;
                }

//                else if (!RegularUtil.checkIdentityId(id)) {
//                    LogUtil.toast(resources.getString(R.string.subscribe_alert_choice_id));
//                    return;
//                }

                LoginModel.DataBean userinfoModel =
                        UserInfoDao.getInstance().queryUserinfoModel();
                if (userinfoModel != null) {
                    Call<SubscribeCommitResponse> commonResponeCall;
                    if (mCacheData == null)
                        commonResponeCall = ApiHelper.subscribeSubmit(mProject.getId(), mShop.getId(), userinfoModel.getId(), mChoiceDate, "", phone, name, mChoiceTime, id, userinfoModel.getAndroidtoken());
                    else
                        commonResponeCall = ApiHelper.subscribeSubmit(mProject.getId(), mShop.getId(), userinfoModel.getId(), mChoiceDate, "", phone, name, mChoiceTime, id, userinfoModel.getAndroidtoken(), mCacheData.getAid());

//                    Log.d(TAG, commonResponeCall.request().url().toString());
//                    String request = ApiHelper.request(commonResponeCall.request().url().toString());
//                    Log.d(TAG, request);

                    commonResponeCall.enqueue(new Callback<SubscribeCommitResponse>() {
                        @Override
                        public void onResponse(@NonNull Call<SubscribeCommitResponse> call, @NonNull Response<SubscribeCommitResponse> response) {
                            final SubscribeCommitResponse body = response.body();
                            if (body == null || body.getCode() == null) return;
                            Log.d(TAG, body.getCode());
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (body.getCode().equals("0") && body.getData() != null) {
                                        Intent intent = new Intent(SubscribeActivity.this, SubscribeInfoActivity.class);
                                        intent.putExtra("ishintshow", true);
                                        intent.putExtra("index", body.getData().getId() + "");//type 为预约信息时，url中为预约信息id
                                        startActivity(intent);
//                                        startActivity(new Intent(SubscribeActivity.this, SubscribeListActivity.class));
                                        clearData();
//                                        finish();
                                    } else {
                                        if (body.getMsg() == null) return;
                                        toast(body.getMsg());
                                    }
                                    missLoadProgressbar();
                                }
                            });

                        }

                        @Override
                        public void onFailure(@NonNull Call<SubscribeCommitResponse> call, @NonNull Throwable t) {
                            Log.d(TAG, "fail");
                            missLoadProgressbar();
                            errorToast();
                        }
                    });
                    showLoadProgresbar();
                } else {
                    Toast.makeText(this, getResources().getString(R.string.common_sign_alert), Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.arrow_down:
            case R.id.tv_location_current:
            case R.id.layout_location:
                startActivity(new Intent(this, SubscribeHistoryAddressActivity.class));
                break;
            case R.id.btn_nav:
                if (mShop == null) return;
                Intent intent = new Intent(this, SubscribeNavActivity.class);
                intent.putExtra("shopid", mShop.getId());
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void showKeyBoard() {
        InputMethodManager imm = (InputMethodManager) edName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

    @Override
    protected void onRestart() {
        if (Build.VERSION.SDK_INT >= 23)
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                if (mPresneter != null && mData == null)
                    mPresneter.location();

        String provinceName = App.preferenceUtil.getString(Constant.PROVINCENAME, null);
        String cityName = App.preferenceUtil.getString(Constant.CITYNAME, "");
        String zoneName = App.preferenceUtil.getString(Constant.ZONENAME, "");
        if (provinceName != null && zoneName != null) {
            Log.d(TAG, zoneName);
            Log.d(TAG, cityName);
            Log.d(TAG, provinceName);
            if (mPresneter != null) {
                mPresneter.addAdd(provinceName, cityName, zoneName);
            }
            refreshCurAdd(provinceName + " " + zoneName);
            mPresneter.getCloseShopInfo(provinceName, cityName, zoneName);
        }

//        showLoadProgresbar();

        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        App.preferenceUtil.putString(Constant.PROVINCENAME, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private class ProjectInnerAdapter extends RecyclerView.Adapter {
        private final SubscribeActivity mActivity;
        private final ArrayList<String> mData;
        private final List<SubscribeIndex.DataBean.SonBean> son;
        private int mIndex;

        ProjectInnerAdapter(SubscribeActivity subscribeActivity, ArrayList<String> list, List<SubscribeIndex.DataBean.SonBean> son) {
            this.mActivity = subscribeActivity;
            this.mData = list;
            this.son = son;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(mActivity, R.layout.item_project_subscribe_text, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ViewHolder vh = (ViewHolder) holder;

            if (mData.get(position) != null) {
                vh.name.setText(mData.get(position));
                vh.name.setTextColor(mActivity.getResources().getColor(R.color.textColorBlack));

                if (mIndex == position)
                    vh.name.setTextColor(mActivity.getResources().getColor(R.color.primarycolor));
                else
                    vh.name.setTextColor(mActivity.getResources().getColor(R.color.textColorBlack));

            }

            vh.clickLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProject = son.get(position);
                    mIndex = position;
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            if (mData == null)
                return 0;
            else
                return mData.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {

            private TextView name;
            private LinearLayout clickLayout;

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                clickLayout = itemView.findViewById(R.id.click_layout);
            }
        }
    }
}
